﻿''' <summary>
''' 投訴匯總報表
''' </summary>
''' <remarks>
''' Create Person:huyang
''' Create Date:2008/05/24
''' </remarks>
Public Class Frm_CSComplaintCollectNew

    Implements IDServer.IDServer.IDServer


#Region "Variabled"

#Region "System Variabled Define"

    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property


#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

    Public FindBool As Boolean = True

    ''' <summary>
    ''' 數據
    ''' </summary>
    ''' <remarks></remarks>
    Private Dtab_Excel As DataTable


#End Region

#End Region

#Region "Methods"

#Region "SetEnabled"


    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type">操作類型</param>
    ''' <param name="IsFind">是否是流覽</param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = True
                        .ToolStripImport.Enabled = False
                        .ToolStripPrint.Enabled = True
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub



#End Region


    Private Sub SetDataGridHeader()

        'Me.DataGridView1.Columns("RowId").HeaderText = "序號"
        'Me.DataGridView1.Columns("fName").HeaderText = "類 別"
        'If Me.DataGridView1.Columns.Contains("A貨") = True Then
        '    Me.DataGridView1.Columns("A貨").HeaderText = "DSC"
        'End If
        Dim Dtab As DataTable = DirectCast(Me.DataGridView1.DataSource, DataTable)
        Dtab.Columns("RowId").ColumnName = "序號"
        Dtab.Columns("fName").ColumnName = "類 別"
        Dtab.Columns("fTotals").ColumnName = "合計"
        If Dtab.Columns.Contains("A貨") = True Then
            Dtab.Columns("A貨").ColumnName = "DSC"
        End If
        Dtab.AcceptChanges()

    End Sub

    Private Sub ComputeDataTable(ByRef Dtab As DataTable)

        For Each row As DataRow In Dtab.Rows
            Dim fTotal As Integer = 0

            For I As Integer = 3 To Dtab.Columns.Count - 1
                Dim fValue As Integer = Val(row(I) & "")

                fTotal += fValue
            Next

            row("fTotals") = fTotal
        Next

        Dtab.AcceptChanges()

        Dim nrow As DataRow = Dtab.NewRow()

        nrow("RowId") = Dtab.Rows.Count + 1
        nrow("fName") = "合計"

        For I As Integer = 3 To Dtab.Columns.Count - 1

            Dim fValue As Integer = Val(Dtab.Compute("sum([" & Dtab.Columns(I).ColumnName & "])", "") & "")

            nrow(Dtab.Columns(I).ColumnName) = IIf(fValue > 0, fValue, System.DBNull.Value)

        Next

        Dtab.Rows.Add(nrow)
        Dtab.AcceptChanges()

    End Sub


    Private Function ReturnDataTabInfo() As DataTable
        Dim dtab As New DataTable
        Dim sql As String = ""
        Dim whereExp As String = Me.ReturnWhereExp()
        Dim SQLStr As New System.Text.StringBuilder
        SQLStr.AppendLine("select  ItemNo=Case when Isnull(ItemNo,'')='001005' and Isnull(ItemName,'')='' then '001005012' --CS來電的其他投訴")
        SQLStr.AppendLine("when Isnull(ItemNo,'')='002005' and Isnull(ItemName,'')='' then '002005012' --CS留言的其他投訴")
        SQLStr.AppendLine("else ItemName end,")
        SQLStr.AppendLine("ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),fSaleOrdeNo,PurchaseDate=Replace(PurchaseDate,'-','/'),")
        SQLStr.AppendLine("SupplierName=Case When SupplierName='A貨' then 'DSC' else SupplierName end,GoodsName,")
        SQLStr.AppendLine("Contents,fEmployeeName,DSCTruck,DGKD,DGDelivery,CustomerName,ContactPhone,")
        SQLStr.AppendLine("LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,")
        SQLStr.AppendLine("LastCallBackRemark=Case When Isnull(LastCallBackRemark,'')='' then Remark else LastCallBackRemark End,")
        SQLStr.AppendLine("CompleteRemark=Case when Isnull(LastCompleteRemark,'')=''   then CompleteRemark else LastCompleteRemark  end ,")
        SQLStr.AppendLine("LastCallBackDate=Case when  (charindex('ok',CompleteRemark,0)>0 or charindex('ok',LastCompleteRemark,0)>0)")
        SQLStr.AppendLine("then (case when Isnull(LastCallBackDate,'')='' then Replace(ReceiveCallDate,'-','/')")
        SQLStr.AppendLine("else Replace(LastCallBackDate,'-','/')   end) else Null end")
        SQLStr.AppendLine("from CS_ReceiveInformation ")
        SQLStr.AppendLine("where Isnull(ItemNo,'')in ('001005','002005') --投訴在CS_CusterServiceTypes中的編號")
        SQLStr.AppendLine("and Isnull(SupplierName,'')<>''")
        SQLStr.AppendLine("and not exists (select * from CS_CusterServiceType where fCode=ItemParentNo)")
        SQLStr.AppendLine(whereExp)

        'sql &= "select  ItemNo=Case when Isnull(ItemNo,'')='' then '0512' else ItemNo end,ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),fSaleOrdeNo,PurchaseDate=Replace(PurchaseDate,'-','/'),SupplierName=Case When SupplierName='A貨' then 'DSC' else SupplierName end,GoodsName," & Chr(13)
        'sql &= "Contents,fEmployeeName,DSCTruck,DGKD,DGDelivery,CustomerName,ContactPhone," & Chr(13)
        'sql &= "LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,LastCallBackRemark=Case When Isnull(LastCallBackRemark,'')='' then Remark else LastCallBackRemark End,CompleteRemark=Case when Isnull(LastCompleteRemark,'')=''   then CompleteRemark else LastCompleteRemark  end ,LastCallBackDate=Case when  (charindex('ok',CompleteRemark,0)>0 or charindex('ok',LastCompleteRemark,0)>0) then (case when Isnull(LastCallBackDate,'')='' then Replace(ReceiveCallDate,'-','/')  else Replace(LastCallBackDate,'-','/')   end) else Null end  " & Chr(13)
        'sql &= "from CS_ReceiveInformation where " & Chr(13)
        'sql &= "Isnull(ItemParentNo,'')='05'   and Isnull(SupplierName,'')<>''    " & Chr(13)
        'sql &= whereExp & Chr(13)
        'sql &= "union   all" & Chr(13)
        'sql &= "select ItemNo=Case when Isnull(ItemNo2,'''')='' then '0512' else ItemNo2 end,ReceiveCallDate=Replace(ReceiveCallDate,'-','/'),fSaleOrdeNo,PurchaseDate=Replace(PurchaseDate,'-','/'),SupplierName=Case When SupplierName='A貨'  then 'DSC'  else SupplierName end,GoodsName," & Chr(13)
        'sql &= "Contents,fEmployeeName,DSCTruck,DGKD,DGDelivery,CustomerName,ContactPhone," & Chr(13)
        'sql &= "LastCallBackPsn=Case when Isnull(LastCallBackPsn,'')='' then ListenPsn else LastCallBackPsn End,LastCallBackRemark=Case When Isnull(LastCallBackRemark,'')='' then Remark else LastCallBackRemark End,CompleteRemark=Case when Isnull(LastCompleteRemark,'')=''   then CompleteRemark else LastCompleteRemark  end ,LastCallBackDate=Case when  (charindex('ok',CompleteRemark,0)>0 or charindex('ok',LastCompleteRemark,0)>0) then (case when Isnull(LastCallBackDate,'')='' then Replace(ReceiveCallDate,'-','/')  else Replace(LastCallBackDate,'-','/')   end) else Null end  " & Chr(13)
        'sql &= "from CS_ReceiveInformation where " & Chr(13)
        'sql &= "Isnull(ItemParentNo,'')='11' and Isnull(ItemNo,'')='1101' and Isnull(ItemParentNo2,'')='05'    and Isnull(SupplierName,'')<>''  " & Chr(13)
        'sql &= whereExp & Chr(13)

        Try
            dtab = Exec(SQLStr.ToString, E_ExecType.DataTable)
        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return dtab

    End Function

    Private Function ReturnWhereExp() As String

        Dim sql As String = ""

        sql &= "  and ReceiveCallDate>='" & Format(Me.dtp_start_1.Value, "yyyy-MM-dd") & "'"

        sql &= "  and ReceiveCallDate<='" & Format(Me.dtp_end_1.Value, "yyyy-MM-dd") & "'"

        If Me.cbxComplainLevel.Text.Trim <> "" Then
            sql &= "  and Isnull(ComplainLevel,'')='" & Me.cbxComplainLevel.Text.Trim & "'"
        End If

        Return sql
    End Function

    Private Function ReturnCaptionHash() As Hashtable
        Dim Hash As New Hashtable

        Hash.Add("ReceiveCallDate", "投訴日期")
        Hash.Add("fSaleOrdeNo", "單據號碼")
        Hash.Add("PurchaseDate", "購買日期")
        Hash.Add("SupplierName", "供應商")
        Hash.Add("GoodsName", "貨品型號")
        Hash.Add("Contents", "顧客投訴事項")
        Hash.Add("fEmployeeName", "員工編號/姓名")
        Hash.Add("DSCTruck", "DSC運輸車")
        Hash.Add("DGKD", "DG 柯打")
        Hash.Add("DGDelivery", "DG 送貨")
        Hash.Add("CustomerName", "顧客姓名")
        Hash.Add("ContactPhone", "顧客電話")
        Hash.Add("LastCallBackPsn", "負責人")
        Hash.Add("LastCallBackRemark", "解決方案")
        Hash.Add("CompleteRemark", "跟進備註")
        Hash.Add("LastCallBackDate", "完成日期")

        Return Hash
    End Function

    Private Sub BindColumnName(ByRef Dtab As DataTable, ByVal Hash As Hashtable)
        Try
            For Each Col As DataColumn In Dtab.Columns
                If Hash.ContainsKey(Col.ColumnName) Then
                    Col.Caption = Hash(Col.ColumnName)
                End If
            Next
            Dtab.AcceptChanges()
        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Function UnionDataTable(ByVal dt1 As DataTable, ByVal dt2 As DataTable) As DataTable
        Dim rDt As DataTable = dt1.Clone
        '刪除dt1中的總計行
        dt1.AcceptChanges()
        dt1 = dt1.Copy()
        Dim drows() As DataRow = dt1.Select("[類 別]='合計'")
        For index As Integer = 0 To drows.Length - 1
            drows(index).Delete()
        Next
        dt1.AcceptChanges()
        '刪除dt2中的總計行
        dt2.AcceptChanges()
        dt2 = dt2.Copy()
        Dim drows2() As DataRow = dt2.Select("[類 別]='合計'")
        For index As Integer = 0 To drows2.Length - 1
            drows2(index).Delete()
        Next
        dt2.AcceptChanges()

        For Each col As DataColumn In dt2.Columns
            If Not rDt.Columns.Contains(col.ColumnName) Then
                rDt.Columns.Add(col.ColumnName, col.DataType)
            End If
        Next
        rDt.Columns("序號").SetOrdinal(0)
        rDt.Columns("類 別").SetOrdinal(1)
        If rDt.Columns.Contains("ItemNo") Then
            rDt.Columns("ItemNo").SetOrdinal(2)
        End If

        For I As Integer = 3 To rDt.Columns.Count - 1
            rDt.Columns(I).DataType = GetType(Double)
        Next
        If rDt.Columns.Contains("合計") Then
            rDt.Columns.Remove("合計")
        End If
        rDt.AcceptChanges()

        '不需要相加的列名列表
        Dim NoAddCols As String = "|序號|類 別|合計|"

        '所有在dt1中存在的記錄添加到新表中，如果dt2中存在同樣的記錄行，那么對數據進行合并
        For Each row1 As DataRow In dt1.Rows
            Dim row As DataRow = rDt.NewRow()

            '使用dt1的數據給新行（row）賦值
            For Each col As DataColumn In rDt.Columns
                Dim colName As String = col.ColumnName
                If dt1.Columns.Contains(colName) Then
                    If NoAddCols.Contains(colName) Then
                        row(colName) = IIf(isNull(row1(colName).ToString.Trim), "0", row1(colName).ToString).ToString
                    Else
                        row(colName) = row(colName) + IIf(row1(colName) Is Nothing OrElse Not Double.TryParse(row1(colName).ToString, New Double), "0", row1(colName).ToString).ToString
                    End If
                End If
            Next

            '如果dt2中同時也存在該行，那么將該行的數據合并到新行
            Dim rows2() As DataRow = dt2.Select("[類 別]='" + row1("類 別").ToString + "'")
            If rows2.Length > 0 Then
                For Each col As DataColumn In rDt.Columns
                    Dim colName As String = col.ColumnName
                    If dt2.Columns.Contains(colName) Then
                        If NoAddCols.Contains(colName) Then
                            row(colName) = rows2(0)(colName).ToString
                        Else
                            row(colName) = row(colName) + IIf(rows2(0)(colName) Is Nothing OrElse Not Double.TryParse(rows2(0)(colName).ToString, New Double), "0", rows2(0)(colName).ToString).ToString
                        End If
                    End If
                Next
            End If
            rDt.Rows.Add(row)
        Next

        '還有一部分數據沒有出現在dt1中，只出現在dt2中，那么也需要將它添加到新表中
        For Each row2 As DataRow In dt2.Rows
            Dim rows() As DataRow = rDt.Select("[類 別]='" + row2("類 別").ToString + "'")
            If rows.Length = 0 Then
                Dim row As DataRow = rDt.NewRow()

                '使用dt2的數據給新行（row）賦值
                For Each col As DataColumn In rDt.Columns
                    Dim colName As String = col.ColumnName
                    If dt2.Columns.Contains(colName) Then
                        If NoAddCols.Contains(colName) Then
                            row(colName) = row2(colName).ToString
                        Else
                            row(colName) = row(colName) + IIf(row2(colName) Is Nothing OrElse Not Double.TryParse(row2(colName).ToString, New Double), "0", row2(colName).ToString).ToString
                        End If
                    End If
                Next
                rDt.Rows.Add(row)
            End If
        Next

        rDt.Columns.Add("合計", GetType(Double))
        rDt.Columns("合計").SetOrdinal(rDt.Columns.Count - 1)
        rDt.AcceptChanges()

        '對數據進行匯總
        Dim count As Int32 = 1
        For Each row As DataRow In rDt.Rows
            Dim fTotal As Integer = 0

            For I As Integer = 3 To rDt.Columns.Count - 2
                row(I) = IIf(Val(row(I).ToString) > 0, Val(row(I).ToString), System.DBNull.Value)
                Dim fValue As Integer = Val(row(I) & "")
                fTotal += fValue
            Next
            row("序號") = count
            row("合計") = fTotal
            count = count + 1
        Next
        rDt.Columns("合計").SetOrdinal(rDt.Columns.Count - 1)
        rDt.AcceptChanges()

        Dim nrow As DataRow = rDt.NewRow()

        nrow("序號") = rDt.Rows.Count + 1
        nrow("類 別") = "合計"

        For I As Integer = 3 To rDt.Columns.Count - 1

            Dim fValue As Integer = Val(rDt.Compute("sum([" & rDt.Columns(I).ColumnName & "])", "") & "")

            nrow(rDt.Columns(I).ColumnName) = IIf(fValue > 0, fValue, System.DBNull.Value)

        Next
        rDt.Rows.Add(nrow)

        rDt.AcceptChanges()
        Return rDt
    End Function

#End Region



#Region "Events"

    Private Sub btnImportAndMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportAndMerge.Click
        If Not TypeOf Me.DataGridView1.DataSource Is DataTable Then
            Exit Sub
        End If

        Dim dt As DataTable = CType(Me.DataGridView1.DataSource, DataTable)
        Dim ImportForm As New Frm_ImportExcel
        Dim ds As DataSet
        If ImportForm.ShowDialog() = DialogResult.OK Then
            ds = ImportForm.Ds
        Else
            Exit Sub
        End If
        If ds Is Nothing OrElse ds.Tables Is Nothing OrElse ds.Tables(0) Is Nothing OrElse ds.Tables(0).Columns.Count < 3 OrElse Not ds.Tables(0).Columns(1).ColumnName.Equals("類 別", StringComparison.InvariantCultureIgnoreCase) Then
        Else
            Me.DataGridView1.DataSource = Me.UnionDataTable(dt, ds.Tables(0))
            Me.DataGridView1.Columns("合計").DisplayIndex = Me.DataGridView1.Columns.Count - 1
        End If
    End Sub

    Private Sub btn_Find_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Find_1.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim sql As String = ""
            Dim whereExp As String = " "
            whereExp &= "   and ReceiveCallDate>=''" & Format(Me.dtp_start_1.Value, "yyyy-MM-dd") & "''"
            whereExp &= "   and ReceiveCallDate<=''" & Format(Me.dtp_end_1.Value, "yyyy-MM-dd") & "'' "

            If Me.cbxComplainLevel.Text.Trim <> "" Then
                whereExp &= "  and Isnull(ComplainLevel,'''')=''" & Me.cbxComplainLevel.Text.Trim & "''"
            End If

            sql &= "  Exec [usp_ComplainAnalysisNew]   " & " '" & whereExp & "' "

            Dim Dtab As New DataTable
            Dtab = Exec(sql, E_ExecType.DataTable)
            Dtab.Columns.Add("fTotals", GetType(Integer))
            ComputeDataTable(Dtab)

            Me.DataGridView1.Columns.Clear()
            Me.DataGridView1.DataSource = Dtab
            SetDataGridHeader()
            FullFormatDataGridView(Me.DataGridView1, False, False)
            Me.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells)
            Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect

            Me.DataGridView1.Columns("ItemNo").Visible = False
            Me.DataGridView1.Update()
            Me.DataGridView1.Refresh()
            If Me.fLoadBool = True Then
                Me.SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub Frm_CSComplaintCollect_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.fLoadBool = False
        Me.dtp_start_1.Value = ServerDate
        Me.dtp_end_1.Value = ServerDate
        FillComboxForEnum(Me.cbxComplainLevel, GetType(ModEnum.ComplainLevel))
        Me.cbxComplainLevel.Text = ModEnum.ComplainLevel.嚴重.ToString()
        Me.fLoadBool = True
    End Sub

#End Region

#Region "Interface"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        SelectObject.ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport
        Try
            Me.Cursor = Cursors.WaitCursor
            If Not TypeOf Me.DataGridView1.DataSource Is DataTable Then
                Return False
            End If
            Dim Grid As DataGridView = Me.DataGridView1
            Dim ds As New DataSet
            Dim Dtab As DataTable
            Dtab = DirectCast(Grid.DataSource, DataTable).Copy

            Dtab.TableName = "總表"
            Dim Hash As New Hashtable
            RemoveHideColumn(Grid, Dtab)

            Dim fCaption As String = DatePart(DateInterval.Month, Me.dtp_end_1.Value).ToString & "月份投訴分類統計"

            If Me.CheckBox1.Checked = True Then

                Me.Dtab_Excel = Me.ReturnDataTabInfo()
                Dim tb As DataTable = DirectCast(Me.DataGridView1.DataSource, DataTable)

                BindColumnName(Dtab_Excel, Me.ReturnCaptionHash())
                Dim tempdb As DataTable

                For I As Integer = tb.Columns.Count - 2 To 3 Step -1
                    tempdb = Me.Dtab_Excel.Clone()
                    tempdb.TableName = tb.Columns(I).ColumnName

                    Dim rows() As DataRow = Me.Dtab_Excel.Select("SupplierName='" & tb.Columns(I).ColumnName & "'")

                    For Each row As DataRow In rows
                        tempdb.ImportRow(row)
                    Next
                    tempdb.Columns.Remove("ItemNo")
                    tempdb.AcceptChanges()
                    ds.Tables.Add(tempdb)
                Next

                For I As Integer = tb.Rows.Count - 2 To 0 Step -1
                    tempdb = Me.Dtab_Excel.Clone()
                    tempdb.TableName = tb.Rows(I).Item("類 別") & ""

                    Dim rows() As DataRow = Me.Dtab_Excel.Select("ItemNo like '*" & tb.Rows(I).Item("ItemNo") & "'")
                    For Each row As DataRow In rows
                        tempdb.ImportRow(row)
                    Next

                    tempdb.Columns.Remove("ItemNo")
                    tempdb.AcceptChanges()
                    ds.Tables.Add(tempdb)
                Next

                ds.Tables.Add(Dtab)
                ds.AcceptChanges()
                Dim export As New ExcelExport()
                export.ExprotToExcel_ComplaintanalyzeDs(ds, fCaption)

            Else
                Dim export As New ExcelExport()
                export.ExprotToExcel_Complaintanalyze(Dtab, fCaption)
            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region


End Class
